/*
 * PerformanceController
 *
 * Copyright (c) 2018 Manulife International Ltd.
 *
 * Description: 
 *
 * Maintenance History
 *
 * YYMMDD  Who              Reason
 * ======= ================ ====================================================
 * 180411  Cherry Xu        Application development
 */
package com.manulife.hk.performance.services;

import com.manulife.hk.performance.domains.SubmitSaleEntity;
import com.manulife.hk.performance.repository.SubmitSaleRepository;

import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.ArrayList;
import java.util.List;

@Service
public class SubmitSaleServiceImpl implements SubmitSaleService{
	
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SubmitSaleRepository submitSaleRepository;

    @SuppressWarnings("unchecked")
	@Override
    public List<SubmitSaleEntity> addList(List<SubmitSaleEntity> submitSales) {
    	Iterable<SubmitSaleEntity> savedList = submitSaleRepository.save(submitSales);
    	return IteratorUtils.toList(savedList.iterator());
    }

    @Override
    public void remove(Long id) {
    	SubmitSaleEntity entity = submitSaleRepository.findOne(id);
    	if(null == entity) {
    		throw new EntityNotFoundException("SubmitSale record not found: id="+id);
    	}
    	log.info("SubmitSale record to be deleted", entity.toString());
        submitSaleRepository.delete(id);
    }

    @Override
    public List<SubmitSaleEntity> findAll(SubmitSaleEntity submitSale) {
        return submitSaleRepository.findAll(new Specification<SubmitSaleEntity>() {
            @Override
            public Predicate toPredicate(Root<SubmitSaleEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = getQueryCondition(submitSale, root, cb);
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
               /* Predicate p1 = criteriaBuilder.equal(root.get("id").as(String.class), performance.getId());
                Predicate p2 = criteriaBuilder.equal(root.get("contractNum").as(String.class), performance.getContractNum());
                Predicate p3 = criteriaBuilder.equal(root.get("agentCode").as(String.class), performance.getAgentCode());
                Predicate p4 = criteriaBuilder.equal(root.get("lob").as(String.class), performance.getLob());
                Predicate p5 = criteriaBuilder.equal(root.get("planCode").as(String.class), performance.getPlanCode());
                Predicate p6 = criteriaBuilder.equal(root.get("salesSource").as(String.class), performance.getSalesSource());
                query.orderBy(criteriaBuilder.desc(root.get("id").as(Long.class)));
                query.where(criteriaBuilder.and(p1,p2,p3,p4,p5,p6));
                return query.getRestriction();*/
            }
        });
    }

    private List<Predicate> getQueryCondition(SubmitSaleEntity submitSale, Root<SubmitSaleEntity> root, CriteriaBuilder cb) {

        List<Predicate> list = new ArrayList<>();
        if (null != submitSale.getId()) {
            list.add(cb.equal(root.get("id").as(Long.class), submitSale.getId()));
        }
        if (StringUtils.isNotBlank(submitSale.getAgentCode())) {
            list.add(cb.equal(root.get("agentCode").as(String.class), submitSale.getAgentCode()));
        }
        if (StringUtils.isNotBlank(submitSale.getContractNum())) {
            list.add(cb.equal(root.get("contractNum").as(String.class), submitSale.getContractNum()));
        }
        if (StringUtils.isNotBlank(submitSale.getLob())) {
            list.add(cb.equal(root.get("lob").as(String.class), submitSale.getLob()));
        }
        if (StringUtils.isNotBlank(submitSale.getSalesSource())) {
            list.add(cb.like(root.get("salesSource").as(String.class), submitSale.getSalesSource()));
        }
        return list;
    }

    /**
     * Get Submit Sale Record by submitSalesKey and submitSalesSubKey
     *
     * @param submitSalesKey String
     * @param submitSalesSubKey String
     * @return SubmitSaleEntity object
     */
	@Override
	public SubmitSaleEntity findBySubmitSalesKeyAndSubmitSalesSubKey(String submitSalesKey, String submitSalesSubKey) {
		return submitSaleRepository.findBySubmitSalesKeyAndSubmitSalesSubKey(submitSalesKey, submitSalesSubKey);
	}
	
	 /**
     * Get Submit Sale Record by submitSalesKey
     *
     * @param submitSalesKey String
     * @return SubmitSaleEntity object
     */
	@Override
	public List<SubmitSaleEntity> findBySubmitSalesKey(String submitSalesKey) {
		return submitSaleRepository.findBySubmitSalesKey(submitSalesKey);
	}
}
